package com.hqyj.javaSpringBoot.config;

import com.hqyj.javaSpringBoot.filter.ParameterFilter;
import com.hqyj.javaSpringBoot.interceptor.RequestViewInterceptor;
import com.hqyj.javaSpringBoot.listener.LogListener;
import com.hqyj.javaSpringBoot.modules.common.entiy.ResourceConfigBean;

import java.util.EventListener;

import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @AutoConfigureAfter在加载配置的类之后再加载当前类
 */
@Configuration
@AutoConfigureAfter({WebMvcAutoConfiguration.class})
public class WeMVCbConfig implements WebMvcConfigurer {
    //获取application中自定义的值
    @Value("${server.http.port}")
    private int httpProt;
    //自定义的本地资源路径配置
    @Autowired
    private ResourceConfigBean resourceConfigBean;
    @Autowired
    private RequestViewInterceptor requestViewInterceptor;

    /**
     * springboot，Bean 自动配置，完全采用注解化
     *  对 Jar 包对象的引用，无需再配置 Bean 对 象，可直接注入
     * @return Connector连接器
     */
    @Bean
    public Connector connector(){
        Connector connector=new Connector();
        connector.setScheme("http");
        connector.setPort(httpProt);
        return connector;
    }

    /**
     * 重新注册 ServletWebServerFactory Bean
     * 以 TomcatServletWebServerFactory.addAdditionalTomcatConnectors 实现，添加 Http 连接器
     * */
    @Bean
    public ServletWebServerFactory servletWebServerFactory(){
        TomcatServletWebServerFactory tomcat=new TomcatServletWebServerFactory();
        tomcat.addAdditionalTomcatConnectors(connector());
        return tomcat;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String osName=System.getProperty("os.name");
        if (osName.toLowerCase().startsWith("win")){
            registry.addResourceHandler(resourceConfigBean.getResourcePathPattern()).
                    addResourceLocations(ResourceUtils.FILE_URL_PREFIX+resourceConfigBean.getLocalPathForWindow());
        }else {
            registry.addResourceHandler(resourceConfigBean.getResourcePathPattern()).
                    addResourceLocations(ResourceUtils.FILE_URL_PREFIX+resourceConfigBean.getLocalPathForLinux());
        }
    }
    
//  @Bean
//  public ServletListenerRegistrationBean<EventListener> servletListenerRegistrationBean() {
//  	ServletListenerRegistrationBean<EventListener> bean = new ServletListenerRegistrationBean<EventListener>();
//  	bean.setListener(new LogListener());
//  	return bean;
//  }

//    @Bean
//    public FilterRegistrationBean filterRegistrationBean(){
//        FilterRegistrationBean bean=new FilterRegistrationBean();
//        bean.setFilter(new ParameterFilter());
//        return bean;
//    }

    /**拦截器为spring的，/**两个*表示拦截所有，过滤器和监听器一个*表示拦截所有 */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(requestViewInterceptor).addPathPatterns("/**");
    }
}
